//
//  ZDLoginView.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDVisitorView.h"
#import "Masonry.h"

@interface ZDVisitorView ()

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UIImageView *homeView;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIButton *registerButton;

@property (nonatomic, strong) UIButton *loginButton;

@property (nonatomic, strong) UIImageView *coverView;

@end

@implementation ZDVisitorView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUpSelf];
        [self setUpSubviews];
    }
    
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setUpSelf];
        [self setUpSubviews];
    }
    
    return self;
}


/**
 *  默认设置
 */
- (void)setUpSelf
{
    self.backgroundColor = [UIColor whiteColor];
}


/**
 *  添加子控件
 */
- (void)setUpSubviews
{
    //设置背景颜色 目的是为了与coverView的颜色混合
    self.backgroundColor = [UIColor colorWithWhite:237 / 255.0 alpha:1.0]; //R、G、B值相同时，可以调用该方法
    
    
    //添加子控件
    [self addSubview:self.iconView];
    //添加遮盖 ———— 必须在iconView的上面
    [self addSubview:self.coverView];
    [self addSubview:self.homeView];
    [self addSubview:self.label];
    [self addSubview:self.registerButton];
    [self addSubview:self.rightButton];
    
    
    //为iconView添加约束
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self).offset(-40);
    }];
    
    //为coverView添加约束
    [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(_registerButton.mas_bottom);
    }];

    //为homeView添加约束
    [_homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.mas_equalTo(_iconView);
    }];
    
    //为label添加约束
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.top.mas_equalTo(_iconView).bottom.offset(16);  //error Attributes should be chained before defining the constraint relation'
        make.top.mas_equalTo(_iconView.mas_bottom).offset(16);
        make.centerX.mas_equalTo(_iconView);
        make.width.mas_equalTo(240);
    }];
    
    //为registerButton添加约束
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_label.mas_bottom).offset(16);
        make.left.mas_equalTo(_label);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
    }];
    
    //为rightButton添加约束
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_label.mas_bottom).offset(16);
        make.right.mas_equalTo(_label);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
    }];
    
    /***  mas_bottom是MASViewAttribute类型 是masonry中定义的约束属性
     
          top right offset... 是MASConstraint类型 是masonry中定义的约束对象
     ***/
    
/*
    //代码添加约束  autolayout
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    self.homeView.translatesAutoresizingMaskIntoConstraints = NO;
    self.label.translatesAutoresizingMaskIntoConstraints = NO;
    self.registerButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    //iconView  添加到共同的父控件
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-40]];
    
    //homeView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_homeView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_homeView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_iconView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    
    //label
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:240]];
    
    //registerButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_registerButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_registerButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_registerButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    
    //rightButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_loginButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    
 */
    
}


#pragma mark - 设置访客视图 提供给外界调用
/**
 *  提供给其他控制器自定义访客视图的方法
 */
- (void) customVisitorView:(NSString *)imageName andTitle:(NSString *)title
{
    //隐藏homeView
    self.homeView.hidden = YES;
    
    //将coverView移到最底层
    [self sendSubviewToBack:self.coverView];
    
    //重新设置设置iconView 和 title
    self.iconView.image = [UIImage imageNamed:imageName];
    
    self.label.text = title;
}


#pragma mark - iconView开始旋转的方法
- (void)startRotation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    animation.toValue = @(M_PI * 2);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 20;
    
    //设置动画停止后不移除  在控制器切换时动画会停止 不移除动画保证在进入首页控制器时，动画继续
    animation.removedOnCompletion = NO;
    
    [self.iconView.layer addAnimation:animation forKey:@"rotation"];
}


/// 暂停旋转
- (void)pauseAnimation
{
    // 记录暂停时间
    CFTimeInterval pauseTime = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    
    // 设置动画速度为0
    self.iconView.layer.speed = 0;
    
    // 设置动画偏移时间
    self.iconView.layer.timeOffset = pauseTime;
}

/// 恢复旋转
- (void)resumeAnimation
{
    // 获取暂停时间
    CFTimeInterval pauseTime = self.iconView.layer.timeOffset;
    
    // 设置动画速度为1
    self.iconView.layer.speed = 1;
    
    self.iconView.layer.timeOffset = 0;
    
    self.iconView.layer.beginTime = 0;
    
    CFTimeInterval timeSincePause = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer: nil] - pauseTime;
    
    self.iconView.layer.beginTime = timeSincePause;
}


#pragma mark - 响应按钮点击事件
- (void)registerButtonClick
{
    if ([self.delegate respondsToSelector:@selector(visitorView:didClickButton:)])
    {
        [self.delegate visitorView:self didClickButton:ZDButtonTypeRegister];
    }
}

- (void)loginButtonClick
{
    if ([self.delegate respondsToSelector:@selector(visitorView:didClickButton:)])
    {
        [self.delegate visitorView:self didClickButton:ZDButtonTypeLogin];
    }
}


#pragma mark 懒加载
- (UIImageView *)iconView
{
    if (!_iconView)
    {
        _iconView = [[UIImageView alloc] init];
        
        _iconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        //设置根据内容大小调整size
        [_iconView sizeToFit];
    }
    
    return _iconView;
}

- (UIImageView *)homeView
{
    if (!_homeView)
    {
        _homeView = [[UIImageView alloc] init];
        
        _homeView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        //设置根据内容大小调整size
        [_homeView sizeToFit];
    }
    
    return _homeView;
}

- (UILabel *)label
{
    if (!_label)
    {
        _label = [[UILabel alloc] init];
        
        _label.text = @"关注一些人，看看有什么惊喜";
        _label.textColor = [UIColor lightGrayColor];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.numberOfLines = 0;    //设置换行
        
        //设置根据内容大小调整size
        [_label sizeToFit];
    }
    
    return _label;
}

- (UIButton *)registerButton
{
    if (!_registerButton)
    {
        _registerButton = [[UIButton alloc] init];
        
        _registerButton.tag = ZDButtonTypeRegister;
        
        [_registerButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        //添加点击事件
        [_registerButton addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        //设置根据内容大小调整size
        [_registerButton sizeToFit];
    }
    
    return _registerButton;
}

- (UIButton *)rightButton
{
    if (!_loginButton)
    {
        _loginButton = [[UIButton alloc] init];
        
        _loginButton.tag = ZDButtonTypeLogin;
        
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        //添加点击事件
        [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        //设置根据内容大小调整size
        [_loginButton sizeToFit];
    }
    
    return _loginButton;
}

- (UIImageView *)coverView
{
    if (!_coverView)
    {
        _coverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    
    return _coverView;
}

@end
